<?php

namespace Data;

class UserFollow extends Base {

	public function follow($userId, $followUserId) {
		$data = array(
			'UserId' => $userId,
			'FollowUserId' => $followUserId,
		);
		$i = $this->where($data)->findOne();
		if ($i) {
			return $i['FollowId'];
		}
		$data['CreateTime'] = time();
		return $this->insert($data);
	}

    public function getFollowUsers($userId, $pager) {
        $condition = array(
            'uf.UserId' => $userId,
        );
        $users = $this->select('u.*')
            ->from('UserFollow as uf')
            ->innerJoin('User as u', 'u.UserId = uf.FollowUserId')
            ->pager($pager, true)
            ->where($condition)
            ->order('uf.FollowId desc')
            ->findAll();
        return $users;
    }

    public function getFansUsers($userId, $pager) {
        $condition = array(
            'uf.FollowUserId' => $userId,
        );
        $users = $this->select('u.*')
            ->from('UserFollow as uf')
            ->innerJoin('User as u', 'u.UserId = uf.UserId')
            ->pager($pager, true)
            ->where($condition)
            ->order('uf.FollowId desc')
            ->findAll();
        return $users;
    }

	public function followDel($userId, $followUserId) {
		$data = array(
			'UserId' => $userId,
			'FollowUserId' => $followUserId,
		);
		return $this->where($data)->del();
	}

    public function getUserFollowNumber($userId) {
        $condition = array(
            'UserId' => $userId,
        );
        $number = $this->where($condition)->count();
        return $number;
    }

	public function getUserFansNumber($userId) {
		$condition = array(
			'FollowUserId' => $userId,
		);
		$number = $this->where($condition)->count();
		return $number;
	}

	public function getByUserIdAndFollowUserId($userId, $followUserId) {
		$condition = array(
			'UserId' => $userId,
			'FollowUserId' => $followUserId,
		);
		return $this->where($condition)->findOne();
	}

    public function delFollowUser($userId, $followUserId) {
        $condition = array(
            'UserId' => $userId,
            'FollowUserId' => $followUserId,
        );
        $this->where($condition)->del();
    }
}